<template>
    <div class="app-container">
      <wms-form v-model="queryParams" :form-list="formList" @query="getList" label-width="108px" @export="handleExport">
      </wms-form>
      <div class="container">
        <div class="table-container">
          <wms-table
              row-key="taskId"
              :loading="loading"
              :data-list="taskList"
              :column="column"
              :pageInfo="queryParams"
              @get-list="getList"
              @row-click="handleRowClick"
          />
        </div>
      <transition name="slide-fade">
      <div class="card-container" v-if="selectedRow">
        <el-card class="detail-card" shadow="hover">
          <template #header>
            <div class="card-header">
              <span>盘点确认</span>
              <el-button
                  type="danger"
                  size="small"
                  icon="Close"
                  circle
                  @click="closeCard"
              />
            </div>
          </template>

          <el-form
              ref="register"
              :model="checkData"
              :rules="registerRules"
              class="detail-form"
              label-position="right"
              label-width="100px"
          >
            <el-form-item label="任务ID" prop="taskId">
              <el-input v-model="checkData.taskId" disabled/>
            </el-form-item>
            <el-form-item label="容器编号" prop="contNo">
              <el-input v-model="checkData.contNo" />
            </el-form-item>
            <el-form-item label="站台编号" prop="locateNo">
              <el-input v-model="checkData.locateNo" />
            </el-form-item>
            <el-form-item label="盘点数量" prop="actlQty">
              <el-input-number v-model="checkData.actlQty" :step-strictly="true" :step="1" :min="0" :max="selectedRow.actlQty"/>
            </el-form-item>
          </el-form>

          <div class="card-footer">
            <el-button type="primary" @click="checkAffirm">确认</el-button>
          </div>
        </el-card>
      </div>
      </transition>
      </div>

     
    </div>
</template>

<script setup name="Check">
import {
  listTask,
  getTask,
  delTask,
  addTask,
  updateTask,
  delCheckTask,
  createCheckTask,
  outboundTask,
  listOutTask, checkTaskAffirm
} from "@/api/wms/check";
import WmsTable from '@/components/WmsTable/index.vue'
import WmsForm from '@/components/WmsForm/index.vue'
import { data, column, formList } from './index.js'
import {ElMessage, ElMessageBox} from "element-plus";
import {ibRegister} from "@/api/wms/ibRegister.js";
const dataReactive = reactive(data);
const { queryParams, form, rules } = toRefs(dataReactive);
const { proxy } = getCurrentInstance();
const { wms_task_status, wms_task_type, wms_task_sub_type } = proxy.useDict('wms_task_status', 'wms_task_type', 'wms_task_sub_type');
const taskList = ref([]);
const loading = ref(true);
// 在原有代码中添加以下内容
const checkData = ref({
  taskId: null,
  locateNo: null,
  actlQty: null,
  contNo: null,
  lotNo: null,
  creator: null
})
const selectedRow = ref(null)
let register = ref()

const registerRules = {
  // taskId: [
  //   { required: true, message: "任务Id不能为空", trigger: "blur" }
  // ],
  contNo: [
    { required: true, message: "容器编号不能为空", trigger: "blur" }
  ],
}

const closeCard = () => {
  // checkData.value = null
  selectedRow.value = null
}

const checkAffirm = () => {
  register.value.validate((valid) => {
    if (valid) {
        checkTaskAffirm(checkData.value).then((response) => {
        ElMessage.success('数据保存成功')
      });
    }
  });
}

/** 查询任务查询列表 */
function getList() {
    loading.value = true;
    listOutTask(queryParams.value).then(response => {
        taskList.value = response.rows;
        queryParams.value.total = response.total;
        loading.value = false;
    });
}

const handleRowClick = (row) => {
  const taskStatus = row.taskStatus
  if (taskStatus == '2' || taskStatus == '4') {
    console.log(wms_task_status.value, taskStatus);
    const taskStatusName = wms_task_status.value.find(item => item.value == taskStatus).label
    // closeCard()
    return ElMessage.warning(`状态为【${taskStatusName}】的任务不能确认`);
  }
  selectedRow.value = { ...row }
  checkData.value.ibDetailId = row.ibDetailId
  checkData.value.inQty = row.ibPlanQty
  checkData.value.lotNo = row.lotNo
  checkData.value.creator = row.creator
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    queryParams.vallue.dateRange = [];
    proxy.resetForm("queryRef");
    handleQuery();
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download('wms/task/export', {
        ...queryParams.value
    }, `task_${new Date().getTime()}.xlsx`)
}

getList();
</script>
<style lang="scss">
 @import url('./index.scss');
</style>